﻿using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace zwWitLink
{
    static class Program
    {
        [DllImport("User32.dll")]
        private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);
        [DllImport("User32.dll")]
        private static extern bool SetForegroundWindow(IntPtr hWnd);
        [DllImport("user32.dll")]
        private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
        [DllImport("user32.dll")]
        private static extern bool IsIconic(IntPtr hWnd);
        private const int SW_RESTORE = 9;
        private const int SW_SHOWNORMAL = 1;
        private static void HandleRunningInstance(Process instance)
        {
            IntPtr hWnd = instance.MainWindowHandle;
            if (IsIconic(hWnd))
                ShowWindowAsync(hWnd, SW_RESTORE);
            ShowWindowAsync(hWnd, SW_SHOWNORMAL);//显示  
            SetForegroundWindow(hWnd);//当到最前端  
            ShowWindow(hWnd, SW_SHOWNORMAL);
        }

        private static Process RuningInstance()
        {
            Process currentProcess = Process.GetCurrentProcess();
            Process[] Processes = Process.GetProcessesByName("中未智联");
            foreach (Process process in Processes)
            {
                if (process.Id != currentProcess.Id)
                {
                    return process;
                }
            }
            return null;
        }  
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Process form = RuningInstance();
            if (form != null)
            {
                HandleRunningInstance(form);
            }
            else
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new zwForm());
            }
        }
    }
}
